<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 管理后台首页
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Loader;
use think\Session;
use think\Db;

class Index extends Controller
{
//    protected static $blacklist = ['download'];

    public function index()
    {
        // 读取数据库模块列表生成菜单项
        $nodes = Loader::model('AdminNode', 'logic')->getMenu();

        // 节点转为树
        $tree_node = list_to_tree($nodes);

        // 显示菜单项
        $menu = [];
        $groups_id = [];
        foreach ($tree_node as $module) {
            if ($module['pid'] == 0 && strtoupper($module['name']) == strtoupper($this->request->module())) {
                if (isset($module['_child'])) {
                    foreach ($module['_child'] as $controller) {
                        $group_id = $controller['group_id'];
                        array_push($groups_id, $group_id);
                        $menu[$group_id][] = $controller;
                    }
                }
            }
        }

        // 获取授权节点分组信息
        $groups_id = array_unique($groups_id);
        if (!$groups_id) {
//            exception("没有权限");
            return $this->view->fetch('role/role');
        }
        $groups = Db::name("AdminGroup")->where(['id' => ['in', $groups_id], 'status' => "1"])->order("sort asc,id asc")->field('id,name,icon')->select();

        $this->view->assign('groups', $groups);
        $this->view->assign('menu', $menu);

        return $this->view->fetch();
    }

    /**
     * 欢迎页
     * @return mixed
     */
    public function welcome()
    {
        // 查询 ip 地址和登录地点
        if (Session::get('last_login_time')) {
            $last_login_ip = Session::get('last_login_ip');
            $last_login_loc = \Ip::find($last_login_ip);

            $this->view->assign("last_login_ip", $last_login_ip);
            $this->view->assign("last_login_loc", implode(" ", $last_login_loc));

        }
        $current_login_ip = $this->request->ip();
        $current_login_loc = \Ip::find($current_login_ip);

        $this->view->assign("current_login_ip", $current_login_ip);
        $this->view->assign("current_login_loc", implode(" ", $current_login_loc));

        // 查询个人信息
        $info = Db::name("admin_user_info")->where("id", UID)->find();
        $this->view->assign("info", $info);

        return $this->view->fetch();
    }

    //实时从后端获取数据
    public function test()
    {
        $data = input();
        $userInfo = Db::name('admin_user_info')->where('id',$data['userId'])->where('realname',$data['user_name'])->find();
        if (!$userInfo){
            return ajax_return_adv_error('不存在该用户');
        }
        //根据用户id去找角色管理
        $userRoleInfo = Db::name('admin_role_user')->where('user_id',$userInfo['id'])->find();
        if (!$userRoleInfo){
            return ajax_return_adv_error('不存在该用户的角色信息');
        }
        $userRole = Db::name('admin_role')->where('id',$userRoleInfo['role_id'])->find();
        if (!$userRole){
            return ajax_return_adv_error('不存在该用户角色');
        }
//        elseif ($userRole['name'] != '车调部'){//判断角色
//            return ajax_return_adv_error('','','','','','',3);
//        }
        //车调部
        //进行中,处于申请中止
        $suspendOrderCount = Db::name('dispatch_traveler_group')->where('dispatch_group_status','proceed')
                                                            ->where('is_suspend',2)
                                                            ->count();
        //统计未分配中还有几条数据未处理
        $counts = Db::name('dispatch_traveler_group')->where('dispatch_group_status','undistributed')->count();
        $driverRefuse = Db::name('driver_refused')->field('id,group_id')->select();
        $groupIds = '';
        if ($driverRefuse){
            foreach ($driverRefuse as $drk=>$drv){
                $groupIds .= $drv['group_id'].',';
            }
        }
        //统计申请中的请假消息
        $leaveManageCount = Db::name('leave')->where('leave_status','apply')->count();

        if ($groupIds){
            $groupIds = rtrim($groupIds,',');
            //统计司机拒单的数量
            $driverRefuseCount = Db::name('dispatch_traveler_group')->where('dispatch_group_status','undistributed')
                                                    ->where('id','in',$groupIds)->count();
        }
        $driverRefuseCount = empty($driverRefuseCount) ? 0:$driverRefuseCount;
        //从消息类型
        $infos = Db::name('information')->field('id,value,role_ids')->select();
        $str = '';
        //判断
        $suspend_order_role = 0;
        $refuse_order_role = 0;
        $leave_manage_role = 0;
        foreach ($infos as $ik=>$iv){
            if (in_array($userRoleInfo['role_id'],explode(',',$iv['role_ids'])) ){
                //中止订单
                if ($iv['value'] == 'suspend_order') {
                    $suspend_order_str = "<br>车辆匹配管理-进行中共有{$suspendOrderCount}条中止订单数据尚未处理";
                    $suspend_order_role = 1;//该角色可以收到中止订单的信息
                }
                //拒绝订单
                if ($iv['value'] == 'refuse_order') {
                    $refuse_order_str = "车调匹配管理中-未分配中有{$driverRefuseCount}条司机取消订单未处理";
                    $refuse_order_role = 1;//该角色可以收到拒绝订单的信息
                }
                //请假申请
                if ($iv['value'] == 'leave_message') {
                    $leave_manage_str = "<br>请假管理中-有{$leaveManageCount}条司机请假申请未处理";
                    $leave_manage_role = 1;//该角色可以收到拒绝订单的信息
                }
            }
        }
        if (empty($suspend_order_role)&&empty($refuse_order_role)&&empty($leave_manage_role)){//该角色没有收到中止订单和拒绝订单的权利
            return ajax_return_adv_error('没有收取消息的权限',2,'','','','',2);
        }
        $str .= empty($suspendOrderCount) ? '': $suspend_order_str.'<br>';
        $str .= (empty($counts)&&empty($driverRefuseCount)) ? '':$refuse_order_str;
        $str .= empty($leaveManageCount) ? '': $leave_manage_str.'<br>';
        if (empty($str)){
            $hint = [
                'status'=>false
            ];
            return ajax_return_adv('没有要处理的数据','','','','',$hint);
        }else{
            $countData['suspendOrderCount'] = empty($suspendOrderCount)?0:$suspendOrderCount;
            $countData['counts'] = empty($counts)?0:$counts;
            $countData['driverRefuseCount'] = $driverRefuseCount;
            $countData['leaveManageCount'] = empty($leaveManageCount)?0:$leaveManageCount;
            $countData['val'] = 3;
            return ajax_return_adv_error($str,'','','','','',$countData);
        }
    }

}